<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<title>添加友情链接</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport"
	content="width=device-width, initial-scale=1, maximum-scale=1">
<link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
<script th:src="@{/layui/layui.js}"></script>
<script type="text/javascript">
layui.use(['form','jquery'],
function(){
    var form = layui.form;
    var $ = layui.jquery;
    var layer = layui.layer;
    var id = '[[${#httpServletRequest.getParameter('id')}]]'; //获取传过来的id值
    var linkName = '[[${#httpServletRequest.getParameter('linkName')}]]'; //获取传过来的类型名称值
    var linkUrl = '[[${#httpServletRequest.getParameter('linkUrl')}]]'; //获取传过来的url
    var orderNum = '[[${#httpServletRequest.getParameter('orderNum')}]]'; //获取传过来的序列
    if(id!=null) {
    	$("#idd").val(id);
	    $("#linkName").val(linkName);
	    $("#linkUrl").val(linkUrl);
	    $("#orderNum").val(orderNum);
    }
    //自定义验证规则  
    form.verify({
        linkName: function(value) {
            if (value.length < 2) {
                return '标题至少得2个字符';
            }
        },
        orderNum: [/^[1-9][0-9]{0,1}$/, '只能是1-99位的数字！']
    });
    form.on('submit(linkForm)',
    function(data) {
        //获取博客标题
        var idd = data.field["idd"]
        //获取博客类别id
        var linkName = data.field["linkName"]
        var linkUrl = data.field["linkUrl"] 
        var orderNum = data.field["orderNum"] 
		$.ajax({
            async: false,
            url: '../admin/link/save',
            data: {
                'id': idd,
                'linkName': linkName,
                'linkUrl': linkUrl,
                'orderNum': orderNum,
            },
            type: 'post',
            success: function(result) {
             	if(typeof result === 'string') {
					   result = JSON.parse(result)
				}
                if (result.success) {
                    layer.alert('操作成功！', {
                        title: '系统提示',
                        icon: 6,
                        closeBtn: 0,
                    },
                    function(index) {
                        layer.close(index); //关闭
                      	$("#linkName").val("");
                      	$("#linkUrl").val("");
                      	$("#orderNum").val("");
                    });
                } else {
                    layer.alert('操作失败。。。', {
                        title: '系统提示',
                        icon: 5,
                        closeBtn: 0,
                    },
                    function(index) {
                        layer.close(index); //关闭
                        $("#linkName").val("");
                      	$("#linkUrl").val("");
                      	$("#orderNum").val("");
                    });
                }
            } // end success
        }); //end ajax
        return false;
    }); //end form.on
});
</script>
</head>
<body>
	<form class="layui-form" action="" id="linkForm" style="margin-left: 35%;margin-top: 10%;">
		<div class="layui-form-item">
			<!-- id无法修改  -->
			<input type="hidden" id="idd" name="idd" value=""> <label
				class="layui-form-label">链接名称：</label>
			<div class="layui-input-block"
				style="width: 200px; margin-top: 15px;">
				<input type="text" name="linkName" required lay-verify="linkName"
					placeholder="名称" autocomplete="off" class="layui-input"
					id="linkName" value="">
			</div>
		</div>
		<div class="layui-form-item">
			<label class="layui-form-label">链接Url：</label>
			<div class="layui-input-block" style="width: 200px">
				<input type="text" name=linkUrl required lay-verify="url"
					placeholder="Url" autocomplete="off" class="layui-input"
					id="linkUrl" value="">
			</div>
		</div>
	<div class="layui-form-item">
			<label class="layui-form-label">链接序号：</label>
			<div class="layui-input-block" style="width: 200px">
				<input type="text" name="orderNum" required lay-verify="orderNum"
					placeholder="序号" autocomplete="off" class="layui-input"
					id="orderNum" value="">
			</div>
		</div>
		<div class="layui-form-item">
				<button class="layui-btn" lay-submit="" lay-filter="linkForm" style="margin-left: 120px">确定</button>
				<button type="reset" class="layui-btn layui-btn-primary" style="margin-left: 50px">清空</button>
		</div>
	</form>
</body>
</html>